package com.changgou.service.goods.controller;

import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Goods;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.service.goods.service.SpuService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/spu")
@CrossOrigin
public class SpuController {
    @Autowired
    private SpuService spuService;

    @GetMapping
    public Result<List<Spu>> findList() {
        List<Spu> spuList = spuService.findAll();
        return new Result<>(true, StatusCode.OK, "查询成功", spuList);
    }

    @GetMapping("/{id}")
    public Result findGoodsById(@PathVariable("id") String id) {
        Goods goods = spuService.findGoodsById(id);
        return new Result<>(true, StatusCode.OK, "查询成功", goods);
    }

    @GetMapping("/findById/{id}")
    public Result findSpuById(@PathVariable("id") String id) {
        Spu spu = spuService.findSpuById(id);
        return new Result<>(true, StatusCode.OK, "查询成功", spu);
    }


    @PostMapping
    public Result add(@RequestBody Goods goods) {
        spuService.add(goods);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    @PutMapping("/{id}")
    public Result update(@RequestBody Goods goods, @PathVariable String id) {
        spuService.update(goods);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    @DeleteMapping("/{id}")
    public Result delById(@PathVariable("id") String id) {
        spuService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @GetMapping("/search")
    public Result<List<Spu>> search(@RequestParam Map search) {
        List<Spu> list = spuService.findList(search);
        return new Result<>(true, StatusCode.OK, "查询成功", list);
    }

    @GetMapping("/search/{page}/{size}")
    public Result<List<Spu>> findPage(@PathVariable int page, @PathVariable int size) {
        Page<Spu> pageInfo = spuService.findPage(page, size);
        PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getResult());
        return new Result<>(true, StatusCode.OK, "查询成功", pageResult);
    }

    @GetMapping("/searchPage/{page}/{size}")
    public Result<List<Spu>> findPage(@RequestParam Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page pageInfo = spuService.findPage(searchMap, page, size);
        PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getResult());
        return new Result<>(true, StatusCode.OK, "查询成功", pageResult);
    }

    @PutMapping("/audit/{id}")
    public Result audit(@PathVariable("id") String id) {
        spuService.audit(id);
        return new Result(true, StatusCode.OK, "商品审核成功");
    }

    @PutMapping("/pull/{id}")
    public Result pull(@PathVariable("id") String id) {
        spuService.pull(id);
        return new Result(true, StatusCode.OK, "商品下架成功");
    }

    @PutMapping("/put/{id}")
    public Result put(@PathVariable("id") String id) {
        spuService.put(id);
        return new Result(true, StatusCode.OK, "商品上架成功");
    }

    @PutMapping("/restore/{id}")
    public Result restore(@PathVariable("id") String id) {
        spuService.restore(id);
        return new Result(true, StatusCode.OK, "商品还原成功");
    }

    @DeleteMapping("/realDel/{id}")
    public Result realDel(@PathVariable("id") String id) {
        spuService.realDel(id);
        return new Result(true, StatusCode.OK, "商品删除成功");
    }


}
